package chatRoom;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class Receiver implements Runnable{
    private int port;
    private DatagramSocket socket;

    public Receiver(int port) throws SocketException {
        this.socket = new DatagramSocket(port);
    }

    @Override
    public void run() {
        while(true){
            try {
                DatagramPacket responsePacket=new DatagramPacket(new byte[4096],4096);
                socket.receive(responsePacket);
                String responseStr=new String(responsePacket.getData(),0,responsePacket.getLength());
                System.out.println("接收信息："+responseStr);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
